﻿using _2112A_2_MES._chubowen;
using _2112A_2_MES._wangweihao;
using _2112A_2_MES._XiangHe;
using _2112A_2_MES.ChuBoWens;
using _2112A_2_MES.SuJiaHao;
using _2112A_2_MES.SuJiaHaos;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace _2112A_2_MES.ChuBoWen
{
    /// <summary>
    /// 客户服务实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "楚博文")]
    public class MdVendorService : ApplicationService, IMdVendorService
    {
        /// <summary>
        /// 客户仓储
        /// </summary>
        private readonly IRepository<MdVendor, int> _mdvendorRepository;
        public MdVendorService(IRepository<MdVendor, int> mdvendorRepository)
        {
            _mdvendorRepository = mdvendorRepository;
        }
        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <param name="vendorcode"></param>
        /// <param name="vendorname"></param>
        /// <param name="vendornick"></param>
        /// <returns>返回客户信息的泛型集合</returns>
        public async Task<InterSelect<object>> GetMdVendorList(string? vendorcode, string? vendorname, string? vendornick, string? vendoren, int pageIndex, int pageSize)
        {
            var mdvendorList = await _mdvendorRepository.GetListAsync();
            if (!string.IsNullOrEmpty(vendorcode))
            {
                mdvendorList = mdvendorList.Where(x => x.VendorCode.Contains(vendorcode)).ToList();
            }
            if (!string.IsNullOrEmpty(vendorname))
            {
                mdvendorList = mdvendorList.Where(x => x.VendorName.Contains(vendorname)).ToList();
            }
            if (!string.IsNullOrEmpty(vendornick))
            {
                mdvendorList = mdvendorList.Where(x => x.VendorNick.Contains(vendornick)).ToList();
            }
            if (!string.IsNullOrEmpty(vendoren))
            {
                mdvendorList = mdvendorList.Where(x => x.VendorEn.Contains(vendoren)).ToList();
            }
            var totalCount = mdvendorList.Count;
            var pageList = mdvendorList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var productClassificationList = AutoMapperExt.MapToList<MdVendor>(pageList);
            var data = new { TotalCount = totalCount, PageList = productClassificationList };
            if (mdvendorList != null)
            {
                return new InterSelect<object>(true, "0", data);
            }
            return new InterSelect<object>(false, "1", null);
        }

        /// <summary>
        /// 新增供应商
        /// </summary>
        /// <param name="mdVendor"></param>
        /// <returns></returns>
        public async Task<int> AddMdVendor(MdVendor mdVendor)
        {
            var result = await _mdvendorRepository.FindAsync(x => x.VendorCode == mdVendor.VendorCode);
            if (result == null)
            {
                mdVendor.CreateBy = "楚博文";
                mdVendor.CreateTime = DateTime.Now;
                await _mdvendorRepository.InsertAsync(mdVendor);
                return 1;
            }
            return 0;
        }

        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteMdVendor(int id)
        {
            await _mdvendorRepository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<MdVendor> GetMdVendorBuBy(int id)
        {
            var mdvendorList = await _mdvendorRepository.GetAsync(p=>p.Id==id);
            return mdvendorList;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="mdVendor"></param>
        /// <returns></returns>
        public async Task<int> UpdateMdVendor(MdVendorDTO mdVendor)
        {
            var result = await _mdvendorRepository.GetAsync(mdVendor.Id);
            result.VendorCode = mdVendor.VendorCode;
            result.VendorName = mdVendor.VendorName;
            result.VendorNick = mdVendor.VendorNick;
            result.VendorDesc = mdVendor.VendorDesc;
            result.VendorLogo = mdVendor.VendorLogo;
            result.VendorLevel = mdVendor.VendorLevel;
            result.VendorScore = mdVendor.VendorScore;
            result.Address = mdVendor.Address;
            result.WebSite = mdVendor.WebSite;
            result.Email = mdVendor.Email;
            result.Tel = mdVendor.Tel;
            result.Contact1 = mdVendor.Contact1;
            result.Contact1Tel = mdVendor.Contact1Tel;
            result.Contact1Email = mdVendor.Contact1Email;
            result.Remark = mdVendor.Remark;
            result.VendorEn = mdVendor.VendorEn;
            result.UpdateBy = "小呆呆";
            result.UpdateTime = DateTime.Now;
            await _mdvendorRepository.UpdateAsync(result);
            return result != null ? 1 : 0;
        }
    }
}
